package com.turkline.CatchMePolygon;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.gl.FPSCounter;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.badlogic.androidgames.framework.math.Vector2;

public class World
{
	public static int STATE_READY = 0;
	public static int STATE_PLAY = 1;
	public static int STATE_GAME_OVER = 2;
	public static int STATE_LEVEL_END = 3;
	public static int STATE_GAME_END = 4;
	
	public static boolean success = false;
	
	FPSCounter counter;
	public Paths paths;
	public List<PathBlock> pathsBlock;
	public Calculations calcs;
	
	public int state;
	public float readyTime = 0; // path in alphasini yavas yavas eksiltmek icin
	public float timer = 60; // deltaTime i kumulatif sekilde topluyo
	public String timeString; // ##:## formatinda saniye ve saliseyi gosteriyo
	public String sifir2; // salise tek haneli olunca 0# yerine sadece # gosteriyo. onun icin bunu koydum
	public int level; //levelSelect ekranindan gelen leveli buna atiyoruz. simdi kullanilmiyo ama daha sonra path icin kullanilacak
	public List<List<PathBlock>> totalPath;
	public Game game;
	public double score;
	public int nextLevel;
	
	public static double BRONZE_SCORE = 50;
	public static double SILVER_SCORE = 75;
	public static double GOLD_SCORE = 90;
	
	public World(int level , Game game)
	{
		this.level = level;
		nextLevel = level+1;
		this.game = game;
		counter = new FPSCounter();
		totalPath = new ArrayList<List<PathBlock>>();
		for(int i = 0 ; i < 32 ; i++)
		{
			totalPath.add(new ArrayList<PathBlock>());
			for(int j = 0 ; j< 20 ; j++)
			{
				totalPath.get(i).add(new PathBlock(false , new Vector2(i*25 + 12.5f , j*24+12)));
			}
		}
		generateLevel();
	}
	
	public void generateLevel()
	{
		state = 0; //0:ready, 1:running, 2:finished
		paths = new Paths(level , this);
		pathsBlock = new ArrayList<PathBlock>();
		for(int i = 0 ; i < paths.levelPathX.length ; i++)
		{
			PathBlock block = new PathBlock(true, new Vector2(paths.levelPathX[i]*25 + 12.5f,paths.levelPathY[i] * 24 +12));
			pathsBlock.add(block);
		}
		counter = new FPSCounter();
		calcs = new Calculations();
		timeString = "60:00";
	}
	
	public void update(float deltaTime , TouchEvent tEvent)
	{
		if(state == World.STATE_PLAY)
		{ 
			if(timer <= 0) {
				timer = 0;
				state = STATE_LEVEL_END;
			}
			timer -= deltaTime;
			int timerInt = (int)(timer*100);
			if(timerInt%100<10){sifir2="0";}else{sifir2="";}
			timeString = "" + timerInt/100 + ":" + sifir2 + timerInt%100 + "";
			
			//testLevelEnd();
		}
		
		TouchEvent e = tEvent;
		if(state == World.STATE_READY) 
		{
			updateReady(e,deltaTime);
			return;
		}
		if(e != null)
		{
			Vector2 tPoint = new Vector2(e.x, 480-e.y);
			
			if(state == World.STATE_PLAY) 
			{
				if((e.type == TouchEvent.TOUCH_DOWN || e.type == TouchEvent.TOUCH_DRAGGED))
				{
					int u = (int)(Math.floor((tPoint.x / 25)));
					int j = (int)(Math.floor((tPoint.y / 24)));
					calcs.littleLines(u, j , this); 
					if(!totalPath.get(u).get(j).drawMe)
						Assets.playSound(Assets.stepSound);
					totalPath.get(u).get(j).drawMe = true;
					
					if(OverlapTester.pointInRectangle(totalPath.get(paths.levelPathX[paths.levelPathX.length - 1]).get(paths.levelPathY[paths.levelPathX.length - 1]).bounds, tPoint)) 
					{
						state = World.STATE_LEVEL_END;
						double totalBlocks = 0;
						double totalTrueBlocks = 0;
						double totalImportantTrueBlocks = 0;
						double importantPathCount = -2;
						for(int i = 0 ; i < 32 ; i++)
						{
							totalPath.add(new ArrayList<PathBlock>());
							for(int t = 0 ; t< 20 ; t++)
							{
								if(totalPath.get(i).get(t).drawMe)
								{
									totalBlocks++;
									if(totalPath.get(i).get(t).isPath)
									{
										totalTrueBlocks++;
										if(totalPath.get(i).get(t).isImportantPath)
										{
											totalImportantTrueBlocks++;
										}
									}
								}
								if(totalPath.get(i).get(t).isImportantPath)
								{
									importantPathCount++;
								}
							}
						}
						score = (totalTrueBlocks/totalBlocks)*(totalImportantTrueBlocks/importantPathCount)*100;
						if(score > 100)
							score = 100;
						
						Settings.record(level, score);
					}
				}
			}
			
			if(e != null && state == World.STATE_GAME_END && e.type == TouchEvent.TOUCH_UP) 
			{
				if(OverlapTester.pointInRectangle(Assets.gameEndClose, tPoint)) 
				{
					game.setScreen(new StartScreen(game, 100, 800, 480));
				}
			}
			
			if(e != null && state == World.STATE_LEVEL_END && e.type == TouchEvent.TOUCH_UP) 
			{ 
				if(OverlapTester.pointInRectangle(Assets.restartRect, tPoint)) 
				{
					Assets.playSound(Assets.clickSound);
					if(score >= BRONZE_SCORE) 
					{
						if(Settings.CURRENT_LEVEL != 20)
						{
							Settings.CURRENT_LEVEL += 1;
							game.setScreen(new GameScreen(game, 1000, 800, 480, Settings.CURRENT_LEVEL));
						}
						else
						{
							state = World.STATE_GAME_END;
						}
					} 
					else 
					{
						game.setScreen(new GameScreen(game, 1000, 800, 480, level));
					}
				}
			}
		}
		
		//counter.logFrame();
	}

	private void testLevelEnd()
	{
		score = 95;
		state = World.STATE_LEVEL_END;
	}

	private void updateReady(TouchEvent e , float deltaTime)
	{
		for(int i = 0 ; i < pathsBlock.size() ; i++)
		{
			pathsBlock.get(i).reduceAlpha(deltaTime / (i+pathsBlock.size()/2) *  pathsBlock.size() );
		}
		if(e != null)
		{
			if((e.type == TouchEvent.TOUCH_DOWN || e.type == TouchEvent.TOUCH_DRAGGED))
			{
				state =  World.STATE_PLAY;
			}
		}
	}
}
